package com.starsoft.smdc.bean;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.starsoft.frame.util.DateUtil;

/**
 * SmdcHoliday entity. @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "smdc_holiday")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE, region="ormCache")
public class SmdcHoliday implements java.io.Serializable {

	// Fields

	private String code;
	private String name;
	private Set<SmdcHolidayDetail> smdcHolidayDetails = new HashSet<SmdcHolidayDetail>(0);

	// Constructors

	/** default constructor */
	public SmdcHoliday() {
	}

	/** minimal constructor */
	public SmdcHoliday(String code) {
		this.code = code;
	}

	/** full constructor */
	public SmdcHoliday(String code, String name, Set<SmdcHolidayDetail> smdcHolidayDetails) {
		this.code = code;
		this.name = name;
		this.smdcHolidayDetails = smdcHolidayDetails;
	}

	// Property accessors
	@Id
	@Column(name = "code", unique = true, nullable = false, length = 10)
	public String getCode() {
		return this.code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	@Column(name = "name", length = 20)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "smdcHoliday")
	@Cache(usage=CacheConcurrencyStrategy.READ_WRITE, region="ormCache")
	public Set<SmdcHolidayDetail> getSmdcHolidayDetails() {
		return this.smdcHolidayDetails;
	}

	public void setSmdcHolidayDetails(Set<SmdcHolidayDetail> smdcHolidayDetails) {
		this.smdcHolidayDetails = smdcHolidayDetails;
	}
	
	//////
	private Map<String, SmdcHolidayDetail> specialDayMap;

	@Transient
	public SmdcHolidayDetail getDetail(Date date){
		String dateKey = DateUtil.toString(date);
		if(specialDayMap==null){
			specialDayMap = new HashMap<String, SmdcHolidayDetail>();
			for(SmdcHolidayDetail detail :  smdcHolidayDetails){
				String key = DateUtil.toString(detail.getSpecialDay());
				specialDayMap.put(key, detail);
			}
		}
		return specialDayMap.get(dateKey);
	}
	
}